package com.wing.product.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseShipmentOrder;
import com.wing.product.model.request.WarehouseShipmentOrderForm;
import com.wing.product.model.response.ShipmentOrderInfoVO;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import com.wing.product.model.response.WarehouseShipmentOrderVO;
import com.wing.product.service.WarehouseProductInventoryService;
import com.wing.product.service.WarehouseShipmentOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
* 发货记录（发货订单）(warehouse_shipment_order)表控制层
*
* @author xxxxx
*/

@Api(value = "托管入库管理", tags = "#/warehouse/product/shipmentOrder")
@RestController("WarehouseShipmentOrderController")
@RequestMapping("/warehouse/shipmentOrder")
@RequiredArgsConstructor
public class WarehouseShipmentOrderController {
/**
* 服务对象
*/
    @Autowired
    private WarehouseShipmentOrderService warehouseShipmentOrderService;

    @Autowired
    private WarehouseProductInventoryService warehouseProductInventoryService;


    @ApiOperation(value = "查询列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseShipmentOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String status,
            String shipOrder,
            String customerName,
            String contactName,
            String waybill,
            String plateNo,
            String sTime,
            String eTime
    ){


        Page<WarehouseShipmentOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
//        paramMap.put("warehouseCustomerId",warehouseCustomerId);
        paramMap.put("status",status);//状态
        paramMap.put("shipOrder",shipOrder);//发货单号
        paramMap.put("customerName",customerName);//客户
        paramMap.put("contactName",contactName);//联系人
        paramMap.put("waybill",waybill);//运单号
        paramMap.put("plateNo",plateNo);//车牌号
        paramMap.put("sTime",sTime);//开始时间
        paramMap.put("eTime",eTime);//结束时间

//        return warehouseShipmentOrderService.pageList4app(pg,paramMap);
        IPage<WarehouseShipmentOrderVO> list = warehouseShipmentOrderService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加发货订单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseShipmentOrderForm.Add.class)  @RequestBody WarehouseShipmentOrderForm warehouseShipmentOrderForm)  {
        try{

            Boolean b =  warehouseShipmentOrderService.save(warehouseShipmentOrderForm);
            if(b){
                return JsonResult.success("添加成功");
            } else {
                return JsonResult.fail("添加失败,捆包号重复");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询发货订单", notes = "搜索")
    @ApiImplicitParams({@ApiImplicitParam(name = "shipmentOrderId", value = "发货单号", required = true, dataType = "String", paramType = "form")})
    @GetMapping("/getInfoByShipmentId")
    public JsonResult<ShipmentOrderInfoVO> getInfoByShipmentId(String shipmentOrderId) {
        ShipmentOrderInfoVO shipmentOrderInfoVO = warehouseShipmentOrderService.getInfoByShipmentId(shipmentOrderId);
        return JsonResult.success(shipmentOrderInfoVO);
    }


    @ApiOperation(value = "详情", notes = "详情")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/info")
    public JsonResult info(Long id ,String waybill) {
//        return JsonResult.success(warehouseShipmentOrderService.findOrderInfoById(id ,"shipment"));
        return JsonResult.success(warehouseShipmentOrderService.info(id ,waybill));
    }

    @ApiOperation(value = "受理", notes = "启用")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/updateStatus")
    public JsonResult updateStatus(Long id , Integer status) {

        if (warehouseShipmentOrderService.updateStatus(id,status)) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }

    @ApiOperation(value = "捆包号列表", notes = "捆包号列表")
    @RequestObjectFieldIgnore({"id"})
    @GetMapping("/detailList")
    public JsonResult detailList(String id ) {
        return JsonResult.success(warehouseProductInventoryService.findByShipmentOrderDetailId(id));
    }

    @ApiOperation(value = "修改订单", notes = "添加")
    @PutMapping("/update")
    public JsonResult update(@Validated(WarehouseShipmentOrderForm.Add.class)  @RequestBody WarehouseShipmentOrderForm warehouseShipmentOrderForm)  {
        try{
            Boolean b =  warehouseShipmentOrderService.update(warehouseShipmentOrderForm);
            if(b){
                return JsonResult.success("修改成功");
            } else {
                return JsonResult.fail("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "撤销", notes = "撤销")
    @GetMapping("/revoke")
    public JsonResult revoke(Long id , Integer status) {
        if (warehouseShipmentOrderService.revoke(id,status)) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }
}
